import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRouterMap = [{
  path: '/login',
  component: () => import('@/views/login/index'),
  hidden: true
},

{
  path: '/404',
  component: () => import('@/views/404'),
  hidden: true
},
{
  path: '/',
  component: Layout,
  redirect: '/dashboard',
  children: [{
    path: 'dashboard',
    name: 'Dashboard',
    component: () => import('@/views/dashboard/index'),
    meta: {
      title: '首页',
      icon: 'dashboard'
    }
  }]
},
{
  path: '/theme-travel',
  component: Layout,
  children: [{
    path: '/theme-travel',
    name: 'ThemeTravel',
    component: () => import('@/views/themeTravel/index'),
    meta: {
      title: '主题旅行管理',
      icon: 'example'
    }
  }]
},
{
  path: '/theme-hotel',
  component: Layout,
  children: [{
    path: '/theme-hotel',
    name: 'ThemeHotel',
    component: () => import('@/views/themeHotel/index'),
    meta: {
      title: '主题酒店管理',
      icon: 'example'
    }
  }]
},
{
  path: '/order',
  component: Layout,
  name: 'Order',
  meta: {
    title: '订单管理',
    icon: 'tree',
    breadcrumb: false
  },
  children: [{
    path: 'theme-order',
    name: 'ThemeOrder',
    redirect: '/order/theme-order/wait-travel',
    component: () => import('@/views/order/themeOrder/index'),
    meta: {
      title: '主题订单',
      icon: 'tree'
    },
    children: [{
      path: 'wait-travel',
      name: 'WaitTravel',
      component: () => import('@/views/order/themeOrder/waitTravel/index'),
      meta: {
        title: '待出行',
        icon: 'table'
      }
    },
    {
      path: 'wait-paid',
      name: 'WaitPaid',
      component: () => import('@/views/order/themeOrder/waitPaid/index'),
      meta: {
        title: '待付款',
        icon: 'table'
      }
    },
    {
      path: 'quited',
      name: 'Quited',
      component: () => import('@/views/order/themeOrder/quited/index'),
      meta: {
        title: '已退团',
        icon: 'table'
      }
    }]
  },
  {
    path: 'hotel-order',
    name: 'HotelOrder',
    redirect: '/order/hotel-order/unpaid',
    component: () => import('@/views/order/hotelOrder/index'),
    meta: {
      title: '酒店订单',
      icon: 'tree'
    },
    children: [
      {
        path: 'unpaid',
        name: 'Unpaid',
        component: () => import('@/views/order/hotelOrder/unpaid/index'),
        meta: {
          title: '未支付',
          icon: 'table'
        }
      },
      {
        path: 'paid',
        name: 'Paid',
        component: () => import('@/views/order/hotelOrder/paid/index'),
        meta: {
          title: '已支付',
          icon: 'table'
        }
      }]
  },
  {
    path: 'customization',
    name: 'Customization',
    component: () => import('@/views/order/customization/index'),
    meta: {
      title: '私人定制',
      icon: 'table'
    }
  }]
},
{
  path: '/theme-hotel/room',
  hidden: true,
  component: Layout,
  beforeEnter: (to, from, next) => {
    if (store.getters.themeHotel.hotel.name) {
      to.meta.title = store.getters.themeHotel.hotel.name
    }
    next()
  },
  children: [{
    path: '/theme-hotel/room/:id',
    component: () => import('@/views/themeHotel/room/index'),
    name: 'Room',
    meta: {
      title: '房间'
    }
  }]
},
{
  path: '/query-order',
  hidden: true,
  component: Layout,
  // beforeEnter: (to, from, next) => {
  //   if (store.getters.themeHotel.hotel.name) {
  //     to.meta.title = store.getters.themeHotel.hotel.name
  //   }
  //   next()
  // },
  children: [{
    path: '/query-order/theme/:id',
    component: () => import('@/views/order/themeOrder/order'),
    name: 'OrderTheme',
    meta: {
      title: '订单'
    }
  }, {
    path: '/query-order/hotel/:id',
    component: () => import('@/views/order/hotelOrder/order'),
    name: 'OrderHotel',
    meta: {
      title: '订单'
    }
  }]
},
{
  path: '/search-order',
  hidden: true,
  component: Layout,
  children: [{
    path: '/search-order/theme/:id',
    component: () => import('@/views/order/themeOrder/searchTable'),
    name: 'SearchOrderTheme',
    meta: {
      title: '搜索主题订单'
    }
  }, {
    path: '/search-order/hotel/:id',
    component: () => import('@/views/order/hotelOrder/searchTable'),
    name: 'SearchOrderHotel',
    meta: {
      title: '搜索主题订单'
    }
  }]
}
]

// 异步挂载的路由
// 动态需要根据权限加载的路由表
export const asyncRouterMap = [
  {
    path: '/account',
    component: Layout,
    children: [{
      path: 'account',
      name: 'Account',
      component: () => import('@/views/account/index'),
      meta: {
        role: ['super-admin'],
        title: '员工找账号管理',
        icon: 'user'
      }
    }]
  },
  {
    path: '/my-info',
    component: Layout,
    name: 'MyInfo',
    meta: {
      role: ['admin', 'super-admin'],
      title: '我的信息',
      icon: 'user',
      breadcrumb: false
    },
    children: [{
      path: 'admin',
      name: 'Admin',
      component: () => import('@/views/myInfo/admin/index'),
      meta: {
        role: ['admin'],
        title: '管理员信息',
        icon: 'user'
      }
    },
    {
      path: 'super-admin',
      name: 'SuperAdmin',
      component: () => import('@/views/myInfo/superAdmin/index'),
      meta: {
        role: ['super-admin'],
        title: '超级管理员信息',
        icon: 'user'
      }
    }]
  },

  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({
    y: 0
  }),
  routes: constantRouterMap
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
